package service;

import dao.TcmDao;
import dao.TcmRelationDao;
import dto.TcmDetailDto;
import pojo.Tcm;
import pojo.TcmRelation;
import util.PageUtil;

import java.util.List;

/**
 *
 * @author lzj
 *
 */
public class TcmService {
    TcmDao tcmDao = new TcmDao();
    TcmRelationDao trDao=new TcmRelationDao();


//    展示列表
    public PageUtil getTcmList(int currentPage){
//        设置一页的记录数
        int pageSize=6;
//        获取总记录数
        int totalNum = tcmDao.getTotalNum();
        PageUtil<Tcm> pu = new PageUtil<>(currentPage, pageSize, totalNum);
        //查当前页数据
        List<Tcm> list = tcmDao.getTcmList(pu.getCurrentPage(),pu.getPageSize());
        //将查询到的指定页码的数据存储到分页工具对象中
        pu.setList(list);
        //将分页工具对象返回
        return pu;

    }


//    关键字查询
    public PageUtil getQueryTcmList(String key,int flag){
//        key：搜索关键字 flag：0为name搜索，1为别名搜索，2为原文搜索
        List<Tcm> list =tcmDao.getQueryTcmList(key,flag);
        int pageSize=6;
        int totalNum=list.size();
        PageUtil<Tcm> pu = new PageUtil<>(1, pageSize, totalNum);
        pu.setList(list);
        return pu;
    }

//    获取详细信息，封装为dto
    public TcmDetailDto getTcmDetail(int tcmid){
        TcmDetailDto detailDto=new TcmDetailDto();
        Tcm tcm=tcmDao.getDetail(tcmid);
        List<TcmRelation> tcmRelationList=trDao.getMessByTcmId(tcmid);
        detailDto.setNumber(tcm.getNumber());
        detailDto.setBjyw(tcm.getBjyw());
        detailDto.setAlias(tcm.getAlias());
        detailDto.setChandi(tcm.getChandi());
        detailDto.setDosage(tcm.getDosage());
        detailDto.setName(tcm.getName());
        detailDto.setNote(tcm.getNote());
        detailDto.setProcessing(tcm.getProcessing());
        detailDto.setTaboo(tcm.getTaboo());
        detailDto.setType(tcm.getType());
        detailDto.setXingwei(tcm.getXingwei());
        detailDto.setZhuzhi(tcm.getZhuzhi());
        detailDto.setTcmRelationList(tcmRelationList);
        return detailDto;
    }


//更新中草药
    public void updateTcm(Tcm tcm){
        tcmDao.updateTcm(tcm);
    }


    //    获取tcm，用于edit
    public Tcm getCurTcm(int id){
        return tcmDao.getDetail(id);
    }

    //添加功能
    public void addTcm(Tcm tcm){
        System.out.println("in service");
        tcmDao.addTcm(tcm);
        System.out.println("out service");
    }

//    删除功能
    public void deleteTcm(int id){
        tcmDao.deleteTcm(id);
    }
}


